package cl.regalloc.parse;

import java.util.HashMap;
import java.util.Set;

public class RegisterFactory {
	private static HashMap<String, Register> registersSet = new HashMap<String, Register>();
	private static Integer vRegCounter = 0;

	public RegisterFactory() {
		super();
	}

	public static Register getRegister(String registerName) {
		if (registersSet.containsKey(registerName)) {
			System.out.printf("Register \"%s\" already exists, returning reference...%n", registerName);
			return registersSet.get(registerName);
		} else {
			System.out.printf("Creating new register \"%s\"...%n", registerName);
			Register currentRegister = new Register(registerName);
			registersSet.put(registerName, currentRegister);
			return currentRegister;
		}

	}

	public static Register getNewVirtualRegister() {
		Register newVReg = new Register(vRegCounter.toString(), true);	// "v" is added automatically
		System.out.printf("New virtual register \"%s\" created...%n", newVReg.toString());
		registersSet.put(newVReg.toString(), newVReg);
		vRegCounter += 1;
		return newVReg;
	}

	public static void applyColours(HashMap<String, Integer> rsColourMap) {
		for (String currentKey : rsColourMap.keySet()) {
			Register currentRegister = registersSet.get(currentKey);
			currentRegister.setColour(rsColourMap.get(currentKey));
		}
	}

	public static Set<String> getRegisterKeys() {
		return registersSet.keySet();
	}
}
